﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class Card : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public int cardValue;
    private int cardUINum;
    public bool isPitchOn;
    public Image img;
    public Image pitchOnImg;

    void Awake()
    {
        img = GetComponent<Image>();
        pitchOnImg = transform.GetChild(0).GetComponent<Image>();
    }

    private void GetCardUINum(int value)
    {
        if (value <= 54)
        {
            cardUINum = value;
        }
        else if (value > 56 && value <= 60)
        {
            cardUINum = value - 56;
        }
        else if (value == 61 || value == 62)
        {
            cardUINum = value - 8;
        }
    }

    public void LoadValueSprite()
    {
        GetCardUINum(cardValue);
        Debug.Log("LoadValueSprite显示牌值" + cardUINum);
        img.sprite = Resources.Load<Sprite>("Card/"+cardUINum);
    }

    public void LoadSpriteValueBaseValue(int value)
    {
        GetCardUINum(value);
        Debug.Log("LoadSpriteValueBaseValue显示牌值" + cardUINum);
        img.sprite = Resources.Load<Sprite>("Card/" + cardUINum);
    }

    public void LoadBackSprite()
    {
        Debug.Log("显示卡背");
        img.sprite = Resources.Load<Sprite>("Card/back");
    }

    public void ShowCardTypeValue()
    {
            LoadValueSprite();
    }

    public void ShowCardTypeBack()
    {
            LoadBackSprite();
    }

    public void ShowPitchOn(bool show)
    {
        if (show)
        {
            pitchOnImg.gameObject.SetActive(true);
            isPitchOn = true;
        }
        else
        {
            pitchOnImg.gameObject.SetActive(false);
            isPitchOn = false;
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        //如果0号位坐的是自己   该牌的拥有者是玩家自己
        if (GameData.GetInstance().locationIndex != -1 && transform.parent.GetComponent<PlayerCardController>().ownPlayerLocationIndex == GameData.GetInstance().locationIndex)           
        {
            //如果是出牌阶段 且没有出牌
            if (GameRoomModel.GetInstance().RoomState == 3 && GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].DealState == 0)
            {
                if (isPitchOn)
                {
                    if (transform.parent.GetComponent<PlayerCardController>().RemovePitchOnCardFromList(cardValue))
                    {
                        ShowPitchOn(false);
                    }
                    else
                    {
                        Debug.Log("移除选中牌" + cardValue + "失败");
                    }
                }
                else
                {
                    if (transform.parent.GetComponent<PlayerCardController>().AddPitchOnCardToList(cardValue))
                    {
                        ShowPitchOn(true);
                    }
                    else
                    {
                        Debug.Log("勾选牌" + cardValue + "失败");
                    }
                }           
            }    
        }

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        
    }
}

